﻿using IOPerformanceTest.Tests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IOPerformanceTest.Utils
{
    /// <summary>
    /// 测试的文件内容。
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 执行次数。
        /// </summary>
        public int RunningTimes { get; set; } = 10;

        /// <summary>
        /// 每次测试是是否重新打开关闭文件。
        /// </summary>
        public bool SingleAccess { get; set; } = true;

        /// <summary>
        /// 输入文件路径。
        /// </summary>
        public string InFilepath { get; set; } = "infile.temp";

        /// <summary>
        /// 输出文件路径。
        /// </summary>
        public string OutFilepath { get; set; } = "outfile.temp";

        /// <summary>
        /// 文件大小 ，单位MB。
        /// </summary>
        public int SizeInMB { get; set; } = 10;

        /// <summary>
        /// 算法的ID。
        /// </summary>
        public int AlgorithmID { get; set; } = 0;

        /// <summary>
        /// 根据编号获得算法。
        /// </summary>
        /// <returns></returns>
        public ITestable GetAlgoritm()
        {
            switch (AlgorithmID)
            {
                case 0: return new FileWriteTest();
                case 1: return new FileStreamWriteTest();
                case 2: return new FileStreamWriteTestSmall();
                default: return new FileStreamWriteTest(); 
            }
        }

        /// <summary>
        /// 从输入字符串解析，格式为：
        /// -i 输入文件
        /// -o 输出文件
        /// -t 执行次数
        /// -s 单次打开关闭
        /// -m 文件大小，单位MB
        /// 示例：-i a.txt -o b.txt -t 50 -s true
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Config Parse(string[] args)
        {
            Config conf;
            try
            {
                conf = new Config();

                for (int i = 0; i < args.Length; i++)
                {
                    string type = args[i].ToLower();
                    string value = i + 1 < args.Length ? args[i + 1] : null;
                    if (!type.StartsWith("-") || type.Length < 2 || value == null || args[i + 1].StartsWith("-"))
                        continue;
                    switch (type[1])
                    {
                        case 'i': conf.InFilepath = value; break;
                        case 'o': conf.OutFilepath = value; break;
                        case 't': conf.RunningTimes = int.Parse(value); break;
                        case 's': conf.SingleAccess = value.ToLower() == "true"; break;
                        case 'm': conf.SizeInMB = int.Parse(value); break;
                        case 'a': conf.AlgorithmID = int.Parse(value); break;
                        default: break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return conf;
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var mi in GetType().GetRuntimeProperties())
                sb.AppendLine($"{mi.Name}\t= {mi.GetValue(this)}");
            return sb.ToString();
        }



    }
}
